/**
 * @author xinjie.txt
 * @time: 2020年7月8日 下午10:52:17
 */
package com.jiepu.exercise.primary.chapter5.exercise3;

/**
 * @author xinjie.txt
 * @Description: 栈 --- 先进后出
 */
public class Stack {
	/**
	 * 对外隐藏 stack[].
	 */
	private int[] stack = new int[500];
	private int i = 0;
	int len = stack.length;

	/**
	 * @param m -- 待压栈数据
	 * @Description: 入栈
	 */
	public void push(int m) {

		// 扩容
		if (i >= len) {
			System.out.println("stack is already full");

			int[] newArr = new int[stack.length * 2];
			System.arraycopy(stack, 0, newArr, 0, stack.length);
			stack = newArr;
		}


		// 未满 压栈
		stack[i++] = m;
	}

	/**
	 * @return
	 * @Description: 出栈
	 */
	public int pop() {
		if (i > 0) {
			return stack[--i];
		} else {
			return -1;
		}
	} // pop
}
